package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.TeacherApplicationStatusEnums;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysAttendanceRecord;
import com.ruoyi.system.domain.SysSubject;
import com.ruoyi.system.domain.SysTeacherScore;
import com.ruoyi.system.mapper.SysAttendanceRecordMapper;
import com.ruoyi.system.service.ISysSubjectService;
import com.ruoyi.system.service.ISysSuperviseService;
import com.ruoyi.system.service.ISysTeacherScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTeacherMapper;
import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.system.service.ISysTeacherService;

/**
 * 家教教师Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysTeacherServiceImpl extends ServiceImpl<SysTeacherMapper, SysTeacher> implements ISysTeacherService
{
    @Autowired
    private SysTeacherMapper sysTeacherMapper;

    @Autowired
    private ISysTeacherScoreService scoreService ;

    @Autowired
    private ISysSubjectService subjectService ;

    /**
     * 查询家教教师
     * 
     * @param id 家教教师主键
     * @return 家教教师
     */
    @Override
    public SysTeacher selectSysTeacherById(Long id)
    {
        return sysTeacherMapper.selectSysTeacherById(id);
    }

    /**
     * 查询家教教师列表
     * 
     * @param sysTeacher 家教教师
     * @return 家教教师
     */
    @Override
    public List<SysTeacher> selectSysTeacherList(SysTeacher sysTeacher)
    {

        if (SecurityUtils.hasRole("teacher")) {
            sysTeacher.setUserId(SecurityUtils.getUserId());
            sysTeacher.setStatus(null);
        }
        if (SecurityUtils.hasRole("common")) {
            sysTeacher.setUserId(null);
            sysTeacher.setStatus(TeacherApplicationStatusEnums.APPLICATION_SUCCESS.getCode());

        }
        if (SecurityUtils.hasRole("parent")) {
            sysTeacher.setUserId(null);
            sysTeacher.setStatus(TeacherApplicationStatusEnums.APPLICATION_SUCCESS.getCode());

        }


        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            sysTeacher.setStatus(null);
            sysTeacher.setUserId(null);
        }


        List<SysTeacher> list = sysTeacherMapper.selectSysTeacherList(sysTeacher);
        for (SysTeacher teacher : list) {
            LambdaQueryWrapper<SysTeacherScore> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysTeacherScore::getUserId ,teacher.getUserId()) ;
            List<SysTeacherScore> scoreList = scoreService.list(wrapper);
            if (CollectionUtils.isNotEmpty(scoreList)) {
                BigDecimal bigDecimal = new BigDecimal(0);
                for (SysTeacherScore sysTeacherScore : scoreList) {
                    bigDecimal = bigDecimal.add(sysTeacherScore.getTeacherScore()) ;
                }
                teacher.setTeacherScore(bigDecimal.divide(new BigDecimal(scoreList.size())).setScale(2 ,BigDecimal.ROUND_HALF_UP));
            }else {
                teacher.setTeacherScore(new BigDecimal("0").setScale(2 ,BigDecimal.ROUND_HALF_UP));
            }
        }

        for (SysTeacher teacher : list) {
            SysSubject subject = subjectService.getById(teacher.getSubjectId());
            teacher.setSubjectName(subject.getName());
        }
        return list;
    }

    /**
     * 新增家教教师
     * 
     * @param sysTeacher 家教教师
     * @return 结果
     */
    @Override
    public int insertSysTeacher(SysTeacher sysTeacher)
    {
        LambdaQueryWrapper<SysTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysTeacher::getUserId ,SecurityUtils.getUserId()) ;
        SysTeacher one = this.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(one)) {
            throw new ServiceException("请勿重复申请!") ;
        }
        sysTeacher.setCreateTime(DateUtils.getNowDate());
        sysTeacher.setStatus(TeacherApplicationStatusEnums.ADMIN_APPLICATION.getCode());
        sysTeacher.setName(SecurityUtils.getLoginUser().getUser().getNickName());
        sysTeacher.setUserId(SecurityUtils.getUserId());
        return sysTeacherMapper.insert(sysTeacher);
    }

    /**
     * 修改家教教师
     * 
     * @param sysTeacher 家教教师
     * @return 结果
     */
    @Override
    public int updateSysTeacher(SysTeacher sysTeacher)
    {
        sysTeacher.setUpdateTime(DateUtils.getNowDate());
        return sysTeacherMapper.updateSysTeacher(sysTeacher);
    }

    /**
     * 批量删除家教教师
     * 
     * @param ids 需要删除的家教教师主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherByIds(Long[] ids)
    {
        return sysTeacherMapper.deleteSysTeacherByIds(ids);
    }

    /**
     * 删除家教教师信息
     * 
     * @param id 家教教师主键
     * @return 结果
     */
    @Override
    public int deleteSysTeacherById(Long id)
    {
        return sysTeacherMapper.deleteSysTeacherById(id);
    }
}
